using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.HPLC;

internal class CustomGraph : ZedGraphControl
{
	private PointF start = new PointF(0f, 0f);

	private PointF end = new PointF(0f, 0f);

	private PointF startset = new PointF(0f, 0f);

	private PointF endset = new PointF(0f, 0f);

	public HPLCIntegerRegions selectRegion;

	private bool bCaptured = false;

	public new static EventHandler RegionChanged;

	private List<HPLCIntegerRegions> _regions = new List<HPLCIntegerRegions>();

	public void SetRegions(HPLCIntegerRegions[] regions)
	{
		_regions.Clear();
		if (regions != null)
		{
			_regions.AddRange(regions);
		}
	}

	public void ChangePosToReal(float posX, out float realX)
	{
		PointF ptp = new PointF(posX, 0f);
		base.GraphPane.ReverseTransform(ptp, out var rx, out var _);
		realX = (float)rx;
	}

	private void ChangeRealToPos(float realX, out float posX)
	{
		PointF ptr = new PointF(realX, 0f);
		posX = base.GraphPane.GeneralTransform(ptr, CoordType.AxisXYScale).X;
	}

	protected override void OnMouseDown(MouseEventArgs e)
	{
		if (e.Button == MouseButtons.Right && selectRegion != null && base.GraphPane.CurveList.Count() > 0)
		{
			double realx = 0.0;
			double realy = 0.0;
			base.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out realx, out realy);
			IPointList pointlist = base.GraphPane.CurveList[0].Points;
			for (int i = 0; i < pointlist.Count; i++)
			{
				if (Math.Abs(realx - pointlist[i].X) < 1.0 && Math.Abs(realy - pointlist[i].Y) < 100.0)
				{
					startset = new PointF((float)pointlist[i].X, (float)pointlist[i].Y);
					bCaptured = true;
					break;
				}
			}
		}
		base.OnMouseDown(e);
	}

	protected override void OnMouseMove(MouseEventArgs e)
	{
		if (e.Button == MouseButtons.Right && selectRegion != null && bCaptured)
		{
			double realx = 0.0;
			double realy = 0.0;
			base.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out realx, out realy);
			using (Graphics gc = CreateGraphics())
			{
				using Pen pen = new Pen(Color.Gray);
				pen.DashStyle = DashStyle.Dot;
				pen.Color = Color.Red;
				if (base.GraphPane.Chart.Rect.Contains(e.Location))
				{
					PointF pt1 = base.GraphPane.GeneralTransform(startset.X, startset.Y, CoordType.AxisXYScale);
					gc.DrawLine(pen, pt1.X, pt1.Y, e.X, e.Y);
				}
			}
			Refresh();
		}
		base.OnMouseMove(e);
	}

	protected override void OnMouseUp(MouseEventArgs e)
	{
		if (e.Button == MouseButtons.Right && selectRegion != null && bCaptured)
		{
			double realx = 0.0;
			double realy = 0.0;
			base.GraphPane.ReverseTransform(new PointF(e.X, e.Y), out realx, out realy);
			IPointList pointlist = base.GraphPane.CurveList[0].Points;
			for (int i = 0; i < pointlist.Count; i++)
			{
				if (!(Math.Abs(realx - pointlist[i].X) < 1.0) || !(Math.Abs(realy - pointlist[i].Y) < 100.0))
				{
					continue;
				}
				endset = new PointF((float)pointlist[i].X, (float)pointlist[i].Y);
				bCaptured = false;
				if (selectRegion != null)
				{
					selectRegion.Start = Convert.ToDouble(startset.X.ToString());
					selectRegion.End = Convert.ToDouble(endset.X.ToString());
					if (RegionChanged != null)
					{
						RegionChanged(null, null);
					}
				}
				break;
			}
		}
		base.OnMouseUp(e);
	}

	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		foreach (GraphPane pane in base.MasterPane.PaneList)
		{
			List<RectangleF> rects = new List<RectangleF>();
			foreach (HPLCIntegerRegions region in _regions)
			{
				if (region.Start < 0.0 || region.End < 0.0)
				{
					continue;
				}
				IPointList pointlist = base.GraphPane.CurveList[0].Points;
				for (int i = 0; i < pointlist.Count - 1; i++)
				{
					if (pointlist[i].X <= region.Start && pointlist[i + 1].X > region.Start)
					{
						start.X = (float)pointlist[i].X;
						start.Y = (float)pointlist[i].Y;
					}
					if (pointlist[i].X <= region.End && pointlist[i + 1].X > region.End)
					{
						end.X = (float)pointlist[i].X;
						end.Y = (float)pointlist[i].Y;
					}
				}
				PointF pt1 = pane.GeneralTransform(start.X, start.Y, CoordType.AxisXYScale);
				PointF pt2 = pane.GeneralTransform(end.X, end.Y, CoordType.AxisXYScale);
				PointF peakpt = pane.GeneralTransform(region.PeakPostion, region.Peak, CoordType.AxisXYScale);
				Pen pen = new Pen(Color.Red, 1f);
				string str = "*";
				Font font = new Font("SimSun", 9f);
				SizeF size = e.Graphics.MeasureString(str, font);
				float xmin = base.GraphPane.Chart.Rect.X;
				float ymin = base.GraphPane.Chart.Rect.Y;
				float width = base.GraphPane.Chart.Rect.Width;
				float height = base.GraphPane.Chart.Rect.Height;
				if (pt1.X > xmin && pt1.X < xmin + width && pt1.Y > ymin && pt1.Y < ymin + height)
				{
					e.Graphics.DrawString(str, font, new SolidBrush(Color.Red), pt1.X - size.Width / 2f, pt1.Y - size.Height / 2f);
				}
				if (pt1.X > xmin && pt1.X < xmin + width && pt1.Y > ymin && pt1.Y < ymin + height)
				{
					e.Graphics.DrawString(str, font, new SolidBrush(Color.Red), pt2.X - size.Width / 2f, pt2.Y - size.Height / 2f);
				}
				if (peakpt.X > xmin && peakpt.X < xmin + width && peakpt.Y > ymin && peakpt.Y < ymin + height)
				{
					e.Graphics.DrawString("|", font, new SolidBrush(Color.Red), peakpt.X - size.Width / 2f, peakpt.Y - size.Height / 2f);
					e.Graphics.DrawString(region.Label, font, new SolidBrush(Color.Red), peakpt.X - size.Width / 2f, peakpt.Y - size.Height / 2f - size.Height);
				}
				e.Graphics.DrawLine(pen, pt1, pt2);
				pen.Dispose();
			}
		}
	}
}
